package com.imooc.alllearning.exception;

import com.imooc.alllearning.common.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获处理器
 * @Author: crowsjian
 * @Date: 2020/3/30 20:27
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 拦截业务类异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = BusinessException.class)
    @ResponseBody
    public ResponseResult businessExceptionHandle(BusinessException e){
        log.info("捕捉到业务类异常：",e);
        return ResponseResult.failure(e.getCode(), e.getMessage());
    }


    /**
     * 拦截运行时异常
     * @param e
     */
    @ExceptionHandler(value = RuntimeException.class)
    @ResponseBody
    public ResponseResult runtimeExceptionHandle(RuntimeException e){
        log.info("捕捉到运行时异常：",e);
        return ResponseResult.failure(ErrorCodeEnum.UNKNOW_ERROR.getCode(), ErrorCodeEnum.UNKNOW_ERROR.getMessage());
    }

    /**
     * 拦截系统级别异常
     * @param th
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    @ResponseBody
    public ResponseResult throwableHandle(Throwable th){
        log.info("捕捉Throwable异常：",th);
        return ResponseResult.failure(ErrorCodeEnum.SYSTEM_ERROR.getCode(), ErrorCodeEnum.SYSTEM_ERROR.getMessage());
    }
}
